<?php
namespace app\common\controller;

use app\BaseController;
use think\facade\View;
use think\facade\Db;

class Admin extends BaseController
{
    protected $cModel;                  //当前控制器关联模型
    protected $modelClass = false;      //数据模型命名空间
    protected $searchArr = [];          //搜索功能数组
    protected $searchTime = '';         //搜索时间
    protected $middleware = ['load_lang_pack', 'admin_auth', 'admin_base_data'];
    
    public function initialize()
    {
        parent::initialize();
        //是否开启浏览模式
        if (config('dbconfig.sys.browse_mode') == 1 && session('admin_id') == 44){
            if (input('post.') || request()->isPost()){
                ajax_return(lang('isbrowse'));
                exit();
            }
        }
        if (class_exists($this->modelClass)){
            $this->cModel = new $this->modelClass;
        }
    }
    
    /**
     * @Title: index
     * @Description: todo(基础单表列表显示)
     * @author 刀客源码 <654108442@qq.com>
     * @date 2018年10月25日
     * @throws
     */
    public function index()
    {
        $where = [];
        
        if (input('get._sort')){
            $order = explode(',', input('get._sort'));
            $order = $order[0].' '.$order[1];
        }else{
            $order = 'id desc';
        }
        $dataList = $this->cModel->where($where)->order($order)->paginate('', false, page_param());
        
        View::assign('dataList', $dataList);
        return View::fetch();
    }
    
    /**
     * @Title: create
     * @Description: todo(基础单表新增)
     * @author 刀客源码 <654108442@qq.com>
     * @date 2018年10月25日
     * @throws
     */
    public function create()
    {
        if (request()->isPost()){
            $data = input('post.', '', 'htmlspecialchars');
            $validate = 'app\common\validate\\'.CONTROLLER_NAME;
            $validate = new $validate;
            if (!$validate->scene('create')->check($data)) {
                return ajax_return($validate->getError());
            }
            $result = $this->cModel->save($data);
            if ($result){
                return ajax_return(lang('action_success'), url('index'));
            }else{
                return ajax_return(lang('action_fail'));
            }
        }else{
            View::assign('data', []);
            return View::fetch('edit');
        }
    }
    
    /**
     * @Title: edit
     * @Description: todo(基础单表编辑)
     * @author 刀客源码 <654108442@qq.com>
     * @date 2018年11月4日
     * @throws
     */
    public function edit()
    {
        if (request()->isPost()){
            $data = input('post.', '', 'htmlspecialchars');
            $id = $data['id'];
            if (count($data) == 2){
                foreach ($data as $k =>$v){
                    $fv = $k!='id' ? $k : '';
                }
                $validate = 'app\common\validate\\'.CONTROLLER_NAME;
                $validate = new $validate;
                if (!$validate->scene($fv)->check($data)) {
                    return ajax_return($validate->getError());
                }
                $result = $this->cModel->where('id', $id)->save($data);
            }else{
                $validate = 'app\common\validate\\'.CONTROLLER_NAME;
                $validate = new $validate;
                if (!$validate->scene('edit')->check($data)) {
                    return ajax_return($validate->getError());
                }
                $result = $this->cModel->where('id', $id)->save($data);
            }
            if ($result){
                return ajax_return(lang('action_success'), url('index'));
            }else{
                return ajax_return(lang('action_fail'));
            }
        }else{
            $id = input('get.id');
            $data = $this->cModel->find($id);
            View::assign('data', $data);
            return View::fetch();
        }
    }
    
    /**
     * @Title: delete
     * @Description: todo(基础单表删除)
     * @author 刀客源码 <654108442@qq.com>
     * @date 2018年11月4日
     * @throws
     */
    public function delete()
    {
        if (request()->isPost()){
            $id = input('id');
            if (isset($id) && !empty($id)){
                $id_arr = explode(',', $id);
                $where[] = ['id', 'in', $id_arr];
                $result = $this->cModel->where($where)->delete();
                if ($result){
                    return ajax_return(lang('action_success'), url('index'));
                }else{
//                     return ajax_return($this->cModel->error());
                    return ajax_return(lang('action_fail'));
                }
            }
        }
    }
    
    /**
     * @Title: _indexSearch
     * @Description: todo(列表搜索功能)
     * @author 刀客源码 <654108442@qq.com>
     * @date 2018年11月4日
     * @throws
     */
    final protected function _indexSearch()
    {
        $this->setLang();
        $searchArr = $this->searchArr;
        if (empty($searchArr)){
            exit('protected $searchArr Empty');
        }
        if (count($searchArr) > 1){
            $searchArr = [implode('|', array_keys($searchArr)) => lang('search')] + $searchArr;
        }
        if (input('param.search_field/s')){
            $topSearchParam['now_field'] = input('param.search_field/s');
        }else{
            $topSearchParam['now_field'] = key($searchArr);
        }
        $topSearchParam['now_lang'] = $searchArr[$topSearchParam['now_field']];
        if (input('param.keyword/s')){
            $topSearchParam['now_keyword'] = input('param.keyword/s');
        }else{
            $topSearchParam['now_keyword'] = '';
        }
        $topSearchParam['searchArr'] = $searchArr;
        if (!empty($this->searchTime)){
            $topSearchParam['searchTime'] = $this->searchTime;
        }
        
        View::assign('topSearchParam', $topSearchParam);
    }
    
    protected function setLang()
    {
        
    }
    
    /**
     * @Title: _getSearch
     * @Description: todo(列表搜索功能条件获取)
     * @return array
     * @author 刀客源码 <654108442@qq.com>
     * @date 2018年11月4日
     * @throws
     */
    final protected function _getSearch()
    {
        $search_field   = input('param.search_field/s');    //下拉搜索字段
        $keyword        = input('param.keyword/s');         //下拉搜索关键字
        $filter_time    = input('param._filter_time/s');    //时间搜索字段
        $time_from      = input('param._time_from/s');      //开始时间
        $time_to        = input('param._time_to/s');        //结束时间
        $map = [];
        //搜索框搜索
        if ($search_field != '' && $keyword !== '') {
            $likeStr = '';
            $search_field = explode('|', $search_field);
            foreach ($search_field as $k => $v){
                $likeStr .= " ".$v." LIKE '"."%$keyword%"."' OR";
            }
            $likeStr = substr($likeStr, 0, -2);
            $map[] = ['', 'exp', Db::raw($likeStr)];
        }
        //时间段搜索
        if ($filter_time != '' && $time_from != '' && $time_to != '') {
            $time_from = strtotime($time_from);
            $time_to = strtotime($time_to) + 86399;
            $map[] = [$filter_time, 'between', [$time_from, $time_to]];
        }
        return $map;
    }
}